package com.ruicar.afs.cloud.bizcommon.face.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.bizcommon.face.dto.TencentCloudNuclearBodyDto;
import com.ruicar.afs.cloud.bizcommon.face.service.TencentCloudNuclearBodyService;
import com.ruicar.afs.cloud.bizcommon.face.util.FaceConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthRequest;
import com.tencentcloudapi.faceid.v20180301.models.DetectAuthResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @ClassName TencentCloudNuclearBodyServiceImpl
 * @Description: 腾讯云核身
 * @Author mingzhi.li
 * @Date 2020/8/19
 * @Version V1.0
 **/
@Slf4j
@Service
@Component
@AllArgsConstructor
public class TencentCloudNuclearBodyServiceImpl implements TencentCloudNuclearBodyService {
    private FaceConfig faceConfig;
    @Override
    public DetectAuthResponse tencentCloudFaceCheck(TencentCloudNuclearBodyDto dto) {
        Credential cred = new Credential(faceConfig.getSecretId(), faceConfig.getSecretKey());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(faceConfig.getUrl());
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        FaceidClient client = new FaceidClient(cred, dto.getRegion(), clientProfile);
        JSONObject json = new JSONObject();
        json.put("IdCard",dto.getIdCard());
        json.put("Name",dto.getName());
        json.put("RuleId",faceConfig.getRuleId());
        DetectAuthRequest req = DetectAuthRequest.fromJsonString(json.toJSONString(), DetectAuthRequest.class);
        DetectAuthResponse resp = null;
        try {
            resp = client.DetectAuth(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return resp;
    }
}
